package com.canbot.u05.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.reception.ReceptionModeSettingActivity;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.FileUtil;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.Utils;


/**
 * 导览模式主界面
 */
public class GuideEnterActivity extends BaseActivityNoTitle {


        Bitmap[] bitmap = new Bitmap[FileUtil.getFilesLength(ConstUtils.SD_PathStartrecive, ".png")];

        StringBuffer micPath = new StringBuffer();

        String path_content = "u5_voicereceiveanim";

        private int i;

        private static final int STARTRECOGNIZER = 3;

        int rdImgCount;

        int rgImgCount;

        // 显示动画
        private ImageView merge;

        private GridView mGridView;

        private int[] icon_ids = {R.drawable.icon_men_welcome_selector, R.drawable.icon_horn_selector, R.drawable.icon_consult_selector};

        private String[] names = {"门童接待", "场馆介绍", "咨询问答"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                //                setContentView(R.layout.act_guide_enter);
                setContentView(R.layout.activity_guide_setting);

                merge = (ImageView) findViewById(R.id.imgview);
                mGridView = (GridView) findViewById(R.id.gridView);

                rdImgCount = FileUtil.getFilesLength(ConstUtils.SD_UuimagestartPath, ".png");
                rgImgCount = FileUtil.getFilesLength(ConstUtils.SD_PathStartrecognize, ".png");

                initData();
        }

        public void initData() {
                mGridView.setAdapter(new OfficeSettingAdapter());
                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                itemClick(position);
                        }
                });
        }

        /**
         * GridView item 点击处理
         *
         * @param position gridView position
         */
        private void itemClick(int position) {
                switch (position) {
                        case 0:
                                guideMenWelcome();
                                break;
                        case 1:
                                horn();
                                break;
                        case 2:
                                guideConsult();
                                break;
                }
        }


        /**
         * 返回
         */
        public void guideBack(View view) {
                finish();
        }

        /**
         * 门童接待
         */
        public void guideMenWelcome() {
                MsgSendUtils.sendStringMsg(MsgType.SEND_PAD_MEETING_START, "handSetting");
                Intent intent = new Intent(this, ReceptionModeSettingActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }

        /**
         * 场馆介绍
         */
        public void horn() {
        }

        /**
         * 咨询问答
         */
        public void guideConsult() {
        }

        /**
         * 舞蹈表演
         */
        public void guidePerform() {
        }

        /**
         * 路径指引
         */
        public void path() {
        }

        /**
         * 会员管理
         */
        public void guideCard() {
        }

        /**
         * 添加动画
         */
        private Handler mHandler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                        setAnimeImage(ConstUtils.SD_PathStartrecive, STARTRECOGNIZER, 60);
                }
        };

        /**
         * 设置动画图片
         */
        private void setAnimeImage(String imgDir, int stateImage, long time) {
                Log.e("setAnimeImage", "setAnimeImage" + i);
                if (bitmap != null && merge != null) {
                        i++;
                        if (i >= bitmap.length) {
                                i = 0;
                        }
                        //            if (i < bitmap.length) {
                        if (micPath != null && micPath.length() > 0) {
                                micPath.delete(0, micPath.length());
                        }
                        micPath.append(path_content).append(i).append(".png");
                        bitmap[i] = Utils.fromSD(imgDir, micPath.toString());
                        micPath.delete(0, micPath.length());
                        if (bitmap[i] != null) {
                                merge.setImageBitmap(bitmap[i]);
                                Logger.d("TAG", "Bitmap[i]" + bitmap[i].toString());
                        }
                        else {
                                Logger.d("TAG", "Bitmap[i]---null");
                        }
                        if (i > 1) {
                                if (null != bitmap[(i - 1)]) {
                                        bitmap[(i - 1)].recycle();
                                        bitmap[(i - 1)] = null;
                                }
                        }
                        mHandler.sendEmptyMessageDelayed(0, time);
                }
        }


        @Override
        protected void onResume() {
                super.onResume();
                setAnimeImage(ConstUtils.SD_PathStartrecive, 2, 60);
                MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "14");
        }

        @Override
        protected void onPause() {
                mHandler.removeMessages(0);
                super.onPause();
        }

        private class OfficeSettingAdapter extends BaseAdapter {

                @Override
                public int getCount() {
                        return icon_ids.length;
                }

                @Override
                public Object getItem(int position) {
                        return position;
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder mHolder;
                        if (convertView == null) {
                                mHolder = new ViewHolder();
                                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_office_setting, null);
                                mHolder.btn = (Button) convertView.findViewById(R.id.btn);
                                mHolder.name = (TextView) convertView.findViewById(R.id.name);
                                convertView.setTag(mHolder);
                        }
                        else {
                                mHolder = (ViewHolder) convertView.getTag();
                        }

                        mHolder.btn.setBackgroundResource(icon_ids[position]);
                        mHolder.name.setText(names[position]);

                        return convertView;
                }

        }

        static class ViewHolder {

                private Button btn;

                private TextView name;
        }

}


